const mapper = require("./mapper.js");
const md5 = require("md5-node")
var session = new Map();
let app = {
    login: (req, res,) => {
        doReq("login",req,res,(data)=>{
            if(data.length != 0){
                let user = md5(JSON.stringify(data));
                let time = new Date().getTime()+30*60*1000;
                session.set(user,time)
                return user+'-'+time; 
            }
            return ;
        });
    },
    test:(req,res)=>{
        res.end(JSON.stringify(success('测试请求')));
    },
    intro:(req,res)=>{
        doReq("intro",req,res);
    },
    education:(req,res)=>{
        doReq("education",req,res);
    },
    works:(req,res)=>{
        doReq("works",req,res);
    },
    skill:(req,res)=>{
        doReq("skill",req,res);
    },
    updateIntro:(req,res)=>{
        tokenCheck(req,res);
        doReq('updateIntro',req,res,(data)=>{
            return data.protocol41 ? '修改成功' :'修改失败';
        });
    },
    insertIntro:(req,res)=>{
        tokenCheck(req,res);
        doReq("insertIntro",req,res,(data)=>{
            return data.protocol41 ? '添加成功' :'添加失败';
        });
    },
    delIntro:(req,res)=>{
        tokenCheck(req,res);
        doReq("delIntro",req,res,(data)=>{
            return data.protocol41 ? '删除成功' :'删除失败';
        });
    },
    insertEducation:(req,res)=>{
        tokenCheck(req,res);
        doReq("insertEducation",req,res,(data)=>{
            return data.protocol41 ? '添加成功' :'添加失败';
        });
    },
    updateEducation:(req,res)=>{ 
        tokenCheck(req,res);
        doReq("updateEducation",req,res,(data)=>{
            return data.protocol41 ? '修改成功' :'修改失败';
        });
    },
    delEducation:(req,res)=>{
        tokenCheck(req,res);
        doReq("delEducation",req,res,(data)=>{
            return data.protocol41 ? '删除成功' :'删除失败';
        });
    },
    insertWork:(req,res)=>{
        tokenCheck(req,res);
        doReq("insertWork",req,res,(data)=>{
            return data.protocol41 ? '添加成功' :'添加失败';
        });
    },
    updateWork:(req,res)=>{
        tokenCheck(req,res);
        doReq("updateWork",req,res,(data)=>{
            return data.protocol41 ? '修改成功' :'修改失败';
        });
    },
    delWork:(req,res)=>{
        tokenCheck(req,res);
        doReq("delWork",req,res,(data)=>{
            return data.protocol41 ? '删除成功' :'删除失败';
        });
    },
    insertSkill:(req,res)=>{
        tokenCheck(req,res);
        doReq("insertSkill",req,res,(data)=>{
            return data.protocol41 ? '添加成功' :'添加失败';
        });
    },
    updateSkill:(req,res)=>{
        tokenCheck(req,res);
        doReq("updateSkill",req,res,(data)=>{
            return data.protocol41 ? '修改成功' :'修改失败';
        });
    },
    delSkill:(req,res)=>{
        tokenCheck(req,res);
        doReq("delSkill",req,res,(data)=>{
            return data.protocol41 ? '删除成功' :'删除失败';
        });
    },
    queryIntro:(req,res)=>{
        tokenCheck(req,res);
        doReq("intro",req,res);
    },
    queryEducation:(req,res)=>{
        tokenCheck(req,res);
        doReq("education",req,res);
    },
    queryWorks:(req,res)=>{
        tokenCheck(req,res);
        doReq("works",req,res);
    },
    querySkill:(req,res)=>{
        tokenCheck(req,res);
        doReq("skill",req,res);
    },
    error: (req, res) => {
        res.end(fail());
    }
}

function doReq(uri,req,res,callback){
    let data = '';
    req.on('data', (chunk) => {
        data += chunk;
    });
    req.on('end',async()=>{
        console.log('route request parm is ->',JSON.stringify(data))
        let resData = await mapper.service(uri, data)
        console.log("route request result is ->",JSON.stringify(resData));
        if(callback){
             res.end(success(callback(resData)))
        }else{
            res.end(success(resData));
        }
    });
}

function tokenCheck(req,res){
    let token = req.headers['authorization'];
    console.log('token->',token)
    if(!token || !checkTime(token)){
        console.log("token check is fail")
        res.end(JSON.stringify( { 'code': 9, 'data': '/login', 'msg': 'fail' }))
    }
}

function checkTime(token){
    let tokens = token.split('-');
    tm = session.get(tokens[0]);
    console.log('tm->',tm)
    let not = new Date().getTime();
    if(!tm || not > new Date(tm*1).getTime()){
        session.delete(tokens[0])
        return false;
    }
    return true;
}

function success(rd) {
    return JSON.stringify( { 'code': 1, 'data': rd, 'msg': 'success' });
}

function fail(rd) {
    return JSON.stringify({ 'code': -1, 'data': rd, 'msg': 'fail' });
}

module.exports = app;
